/**
 * 
 */
package com.axi.resplan;

import java.util.*;


/**
 * @author Akos Szabo - Axi - aaszabo@gmail.com
 *
 * This class loads the list of task and all the parameters.
 * It handles the 
 */
public class TimeTable 
{

	Hashtable _taskList = new Hashtable();
	Vector _taskVector = new Vector();
	
	int _isChanged = 1;
	
	Hashtable _resourceList = new Hashtable();
	Vector _resourceVector = new Vector();
	
	
	Date _earliestDate = new Date(9999,0,0);
	Date _latestDate = new Date(0,0,0);
	
	Date _startDate;
	Date _expectedEndDate;
	
	int _longestWorkTime = 0;
	
	/**
	 * Stores the daterange calculated from earliest and latest Date
	 */
	int _dateRange=0;
	
	
	
	/**
	 * Constructor
	 * 
	 */
	public TimeTable()
	{		
		loadData();		
	}
	

	/**
	 * Loads data in from file, DB, etc.
	 */
	public void loadData()
	{
		_taskList.clear();
		
		//load resource list
		//elég lesz majd csak a Vector
		for(int i =0; i<5; i++)
		{
			_resourceVector.add("Name"+i);
			_resourceList.put("Name"+i, "Name"+i);
		}
		
		//load Task
		Random rand = new Random();

		//create as many tasks as many set in the condition
		for(int i=0; i<55; i++)
		{
			
			//set a random work time in hours needed for the task
			int workTime;			
			if( rand.nextInt(4) == 2 )
			{
				workTime = 10 + rand.nextInt(10); 
			}
			else
			{
				workTime = 1 + rand.nextInt(7);
			}
			workTime *=8;
			
			//find the longest task work time
			if( workTime > _longestWorkTime )
				_longestWorkTime=workTime;
			
			//find the earliest and the latest date
			Date bDate = new Date(111,9,5+rand.nextInt(4));
			Date eDate = new Date(111,10,25+rand.nextInt(4));			
			
			if(eDate.after(_latestDate))
				_latestDate = eDate;			
			
			if(bDate.before(_earliestDate))
				_earliestDate = bDate;
			
			
			Task t = new Task("Task-"+i, 
					""+i, 
					bDate, 
					eDate,
					workTime,
					this);


			//add resource or multiple possible resources to the task			
			int numOf;
			if( rand.nextInt(4) == 2 )//add multiple resources
			{
				 
				//System.out.println("next rnd:"+);
				numOf = rand.nextInt(4) + 1;
				for(int j=0; j<numOf; j++)
				{
					t.addResource("Name"+rand.nextInt(5));
				}
			}
			else//add just one resource
			{
				t.addResource("Name"+rand.nextInt(5));
			}
			
			//put the task into the lists
			_taskList.put("id"+i, t);
			_taskVector.add(t);
		}
		
		
		//calculate the time frame
		int lwt = getLongestWorkTime();		
		Date ed = getEarliestDate();
		Date ld = getLatestDate();
				
		//diff in day
		long diff = (ld.getTime() - ed.getTime()) / (1000 * 60 * 60 * 24);		
		_dateRange = (int)diff + lwt/8 + 1;
				
		System.out.println("date range withs = "+_dateRange);
		
	}//loadData
	
	public Hashtable getTaskList()
	{
		return _taskList;
	}
	
	public Vector getTaskVector()
	{
		return _taskVector;
	}
	

	public Date getEarliestDate()
	{
		return _earliestDate;
	}

	public Date getLatestDate()
	{
		return _latestDate;
	}
	
	public int getLongestWorkTime()
	{
		return this._longestWorkTime;
	}
	
	public int getDateRangeWith()
	{
		return this._dateRange;
	}
	
	public Hashtable getResourceList()
	{
		return _resourceList;
	}
	
	public Vector getResourceVector()
	{
		return _resourceVector;
	}
	
	public void setStartDate( Date argStartDate)
	{
		_startDate = argStartDate;
	}

	public void setExpectedEndDate( Date argEndDate)
	{
		_expectedEndDate = argEndDate;
	}
	
	public Date getStartDate()
	{
		return _startDate;
	}

	public Date getExpectedEndDate()
	{
		return _expectedEndDate;
	}


}//end of class
